package com.zkthink.ceres.applet.reimbursements.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkthink.base.R;
import com.zkthink.ceres.applet.common.domin.PageDate;
import com.zkthink.ceres.applet.reimbursements.domain.vo.AddReimbursementsVo;
import com.zkthink.ceres.applet.reimbursements.domain.vo.ReimbursementsVo;
import com.zkthink.ceres.pay.wechat.v3.service.A;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkthink.ceres.applet.reimbursements.domain.entry.Reimbursements;
import com.zkthink.ceres.applet.reimbursements.mapper.ReimbursementsMapper;
import com.zkthink.ceres.applet.reimbursements.service.ReimbursementsService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.zkthink.ceres.applet.authority.config.AuthorityWebConfiguration.getMid;

/**

@Author cmz
@Date 2024-05-12 16:21
*/
@Service
public class ReimbursementsServiceImpl extends ServiceImpl<ReimbursementsMapper, Reimbursements> implements ReimbursementsService{

    @Resource
    private ReimbursementsMapper reimbursementsMapper;
    @Override
    public R insert(AddReimbursementsVo addAppletFeedBackVo) {
        Reimbursements reimbursements = BeanUtil.toBean(addAppletFeedBackVo, Reimbursements.class);
        reimbursements.setStatus(0);//审核状态
        reimbursements.setCreateUser(getMid());
        reimbursements.setCreateTime(new Date());
        reimbursements.setExpenseImg(String.join(",",addAppletFeedBackVo.getExpenseImgList()));
        int insert = reimbursementsMapper.insert(reimbursements);
        return insert>0?R.success():R.fail("添加失败");
    }

    @Override
    public R findRecord(PageDate pageDate) {
        Long mid = getMid();
        Page<Reimbursements> reimbursementsPage = new Page<>(pageDate.getPageNum(),pageDate.getPageSize());
        QueryWrapper<Reimbursements> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_user",mid);//当前申请人
        queryWrapper.orderByDesc("create_time");
        Page<Reimbursements> reimbursementsPage1 = reimbursementsMapper.selectPage(reimbursementsPage, queryWrapper);
        
        Page<ReimbursementsVo> page = new Page<>();
        page.setTotal(reimbursementsPage1.getTotal());
        page.setSize(reimbursementsPage1.getSize());
        page.setCurrent(reimbursementsPage1.getCurrent());
        page.setOrders(reimbursementsPage1.getOrders());
        page.setPages(reimbursementsPage1.getPages());

        List<Reimbursements> records = reimbursementsPage1.getRecords();
        List<ReimbursementsVo>  list = new ArrayList<>();
        for (Reimbursements record : records) {
            ReimbursementsVo vo = new ReimbursementsVo();
            BeanUtils.copyProperties(record,vo);
            vo.setExpenseImgList(record.getExpenseImg().split(","));
            list.add(vo);
        }
        page.setRecords(list);
        
        return R.success(page);
    }
}
